/**
 * 
 */
package edu.arizona.bio5.onto.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Pavel Klinov
 *
 */
public class IOUtils {
    
	public static String removeExtension(File file) {
		String name = file.getName();
		int index = name.lastIndexOf('.');
		
		return index > 0 ? name.substring(0, index) : name;
	}
	
    public static boolean deleteRecursively(File path) {
    	
        boolean ret = cleanRecursively(path);
        
        return ret && path.delete();
    }
    
    public static boolean cleanRecursively(File path) {
    	
    	if (!path.exists()) return true;
    	
        boolean ret = true;
        
        if (path.isDirectory()){
        	
            for (File f : path.listFiles()){
            	
                ret = ret && IOUtils.deleteRecursively(f);
            }
        }
        
        return ret;
    }   
    
    public static File createNewFile(String fileName) throws IOException {

		File file = new File(fileName);
		
		if (file.exists()) {
			file.delete();
		}

		file.createNewFile();
		
		return file;
	} 
    
    public static void closeQuietly(InputStream stream) {
    	try {
    		if (stream != null) {
    			stream.close();
    		}
    	} catch(Exception e){}
    }
}